--[[surface.CreateFont("Trebuchet19", 15, 1000, true, false, "Text_ChatBox")
chatshow = false

local writetext = ""
local msgtext = ""
local linestoshow = 10
local chathistory = {}
local chatbox

function wrap(str, limit)  
	local Width, Height = surface.GetTextSize(str)
	if string.len(str) > limit then
		return string.sub(str, 1, limit).."\n"..string.sub(str, limit+1)
	else
		return str
	end  
end

function Fluctuate(c)
	return (math.cos(CurTime()*c)+1)/2
end

function TW(s, f) local w = surface.GetTextSize(s, f); return w; end

function _R.Player:IsOwner()
	return (self:SteamID() == "STEAM_0:0:13138982") or (self:SteamID() == "STEAM_0:0:22247737")
end

function SendMessage(pl, msg, typ, color)
	typ = tonumber(type) or 0
		if ValidEntity(pl) then
			local chatInfo = {
				pl = pl,
				owner = pl:IsOwner(),
				superadmin = pl:IsSuperAdmin(),
				admin = pl:IsAdmin(), 
				guest = nil, 
				name = pl:GetName(), 
				msgtextcolor = color, 
				teamcolor = team.GetColor(pl:Team()),
				msg = msg,
				typ = type, 
				time = RealTime()+15
			}; 
		table.insert(chathistory, 1, chatInfo)
	else
			local chatInfo = {
			msgtextcolor = color, 
			msg = msg,
			typ = type, 
			time = RealTime()+15
		};
		table.insert(chathistory, 1, chatInfo)
	end
	chathistory[linestoshow+1] = nil
end

hook.Add("OnPlayerChat", "Text_ChatBox", function(pl, msg, typ, col)
	if (ValidEntity(pl)) then
		SendMessage( pl, msg, 1, (Color( 255, 255, 255, 255)) )
		end
	return pl
end)

local x, y = 80, ScrH()-200
function ChatPaint()
	if ChatOffset then 
		x = ChatOffset.x or x 
		y = ChatOffset.y or y 
	end
	
for i, v in pairs(chathistory) do
	local ptime = v.time > RealTime()
		if ptime or chatshow then
			if v.pl then
				local x2 = x
				if v.owner then
					draw.SimpleText("[Owner]", "Text_ChatBox", x-4, (y-i*15)+1, Color(0, 0, 0, 255), 2)
					draw.SimpleText("[Owner]", "Text_ChatBox", x-5, (y-i*15), Color(Fluctuate(3)*140, 0, Fluctuate(3)*255, 255), 2)
					draw.SimpleText(v.name, "Text_ChatBox", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text_ChatBox", x, y-i*15, v.teamcolor, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
					x2 = x2+TW(v.name, "Text_ChatBox")+1
				elseif v.superadmin then
					draw.SimpleText("[Super]", "Text_ChatBox", x-4, (y-i*15)+1, Color(0, 0, 0, 255), 2)
					draw.SimpleText("[Super]", "Text_ChatBox", x-5, (y-i*15), Color(Fluctuate(3)*255, 0, 0, 255), 2)
					draw.SimpleText(v.name, "Text_ChatBox", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text_ChatBox", x, y-i*15, v.teamcolor, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
					x2 = x2+TW(v.name, "Text_ChatBox")+1
				elseif v.admin then
					draw.SimpleText("[Admin]", "Text_ChatBox", x-4, (y-i*15)+1, Color(0, 0, 0, 255), 2)
					draw.SimpleText("[Admin]", "Text_ChatBox", x-5, (y-i*15), Color(Fluctuate(3)*255, Fluctuate(3)*140, Fluctuate(3)*45, 255), 2)
					draw.SimpleText(v.name, "Text_ChatBox", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text_ChatBox", x, y-i*15, v.teamcolor, TEXT_ALIGN_LEFT, TEXT_ALIGN_LEFT)
					x2 = x2+TW(v.name, "Text_ChatBox")+1				
				elseif v.guest then
					draw.SimpleText(v.name, "Text_ChatBox", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text_ChatBox", x, y-i*15, Color(0, 175, 35, 255))
					x2 = x2+TW(v.name, "Text_ChatBox")+1
				else
					draw.SimpleText(v.name, "Text_ChatBox", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
					draw.SimpleText(v.name, "Text_ChatBox", x, y-i*15, v.teamcolor)
					x2 = x2+TW(v.name, "Text_ChatBox")+1
				end

				draw.DrawText(": "..v.msg, "Text_ChatBox", x2+1, (y-i*15)+1, Color(0, 0, 0, 255))
				draw.DrawText(": "..v.msg, "Text_ChatBox", x2, y-i*15, v.msgtextcolor)
			
			else
				draw.DrawText(v.msg, "Text_ChatBox", x+1, (y-i*15)+1, Color(0, 0, 0, 255))
				draw.DrawText(v.msg, "Text_ChatBox", x, y-i*15, v.msgtextcolor)
			end
		end
	end
	
	if chatshow then
		--surface.SetDrawColor( 0, 0, 0, 255 )
		--surface.DrawOutlinedRect( x - 64, y - 149, 464, 181 )
		--surface.SetDrawColor( 0, 0, 0, 75 )
		--surface.DrawRect( x - 63, y - 148, 462, 179 )
		draw.RoundedBox(6, x-60, y+10, 60, 20, Color(25, 25, 25, 240))
		draw.SimpleText( "Say", "Text_ChatBox", x-20, y+13, Color( 255, 255, 255, 240 ), TEXT_ALIGN_RIGHT, TEXT_ALIGN_RIGHT )
	end
end
hook.Add("HUDPaint", "DrawChat", ChatPaint)
			
hook.Add("ChatText", "ChatMessages", function(plindex, plname, text, typ)
	if typ == "none" then
		SendMessage(nil, text, nil, Color(0, 55, 195, 255))
	elseif typ == "joinleave" then
		SendMessage(nil, text, nil, Color(0, 235, 45, 255))
	end
end)

hook.Add("StartChat", "StartChat", function()
chatshow = true;
	chatbox = vgui.Create("DTextEntry")
	chatbox:SetText("Enter your message here!")
	chatbox:SetWide(400)
	chatbox:SetPos(x-10, y+10)
	chatbox:RequestFocus()
--	chatbox:SetDrawBackground(false)
--	chatbox:SetDrawBorder(false)
	chatbox.OnEnter = function()
	chatbox:SetText("")
	end
	chatbox.OnMousePressed = function()
	chatbox:SetText("")
	end
return true; 
end)
hook.Add("FinishChat", "FinishChat", function() chatshow = false; writetext = ""; msgtext = "";	chatbox:Remove() return false end)
hook.Add("ChatTextChanged", "TextChanged", function(text) writetext = text; msgtext = text; chatbox:SetText(text) end)
hook.Add("PlayerBindPress", "TeamChat", function(pl, bind) if string.find(bind, "messagemode2") then chatshow = true; end end)--]]